package com.lz.yz.reward.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.order.enumeration.OrderStatus;
import com.lz.yz.order.enumeration.OrderType;
import com.lz.yz.reward.model.RewardModel;
import com.lz.yz.reward.param.RewardParam;
import com.lz.yz.reward.service.RewardService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* Created by CodeGenerator on 2017/11/10.
*/
@RestController
@RequestMapping("/reward")
public class RewardController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private RewardService rewardService;

    @Autowired
    HttpServletRequest request;



    @ApiOperation(value="发布找大咖悬赏")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "model", value = "悬赏实体类", dataType = "RewardModel", paramType = "body"),
    })
    @PostMapping
    public ReturnValue add(@RequestBody RewardModel model) {
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            model.setAccountId(accountId);
            return rewardService.add(model);
        } catch (Exception e) {
            logger.error("发布找大咖出错" + model, e);
            return ReturnValue.generateFalseReturnValue("发布找大咖失败");
        }
    }

    @ApiOperation(value="找大咖悬赏详情",response = RewardModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "path",name = "id", value = "悬赏主键"),
    })
    @GetMapping("/{id}")
    public ReturnValue detail(@PathVariable Integer id) {
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            RewardParam param=new RewardParam();
            param.setRewardId(id);
            param.setLoginId(accountId);
            return rewardService.get(param);
        } catch (Exception e) {
            logger.error("找大咖悬赏详情" + id, e);
            return ReturnValue.generateFalseReturnValue("找大咖悬赏详情");
        }

    }

    @ApiOperation(value="查询悬赏列表",response = RewardModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query",name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(paramType = "query",name = "status", value = "状态:1.进行中，2.已完成 3.已取消 4 已解答 5 标识支付状态"),
            @ApiImplicitParam(paramType = "query",name = "type", value = "1我发布的找大咖 2我购买的找大咖 3我参与的找大咖 不传查所有"),
    })
    @GetMapping
    public ReturnValue list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize, String status, Integer type) {
        PageHelper.startPage(page, pageSize);
        RewardParam param=new RewardParam(page,pageSize);
        Integer accountId = (Integer) request.getAttribute("accountId");
        param.setLoginId(accountId);
        if(type!=null){
            switch (type){
                case 1:
                    param.setOrderType(OrderType.REWARD.getStatusValue());
                    param.setPublishAccountId(accountId);
                    break;
                case 2:
                    param.setOrderType(OrderType.GET.getStatusValue());
                    param.setBuyerAccountId(accountId);
                    break;
                case 3:
                    param.setOrderType(OrderType.REWARD.getStatusValue());
                    param.setBuyerAccountId(accountId);
                    break;
            }
            param.setOrderStates(OrderStatus.COMPLETE.getStatusValue()+","+OrderStatus.REFUND.getStatusValue());
        }else {
            param.setRewardStates(status);
            param.setOrderType(OrderType.REWARD.getStatusValue());
            param.setOrderStatus(OrderStatus.COMPLETE.getStatusValue());
        }
        return rewardService.list(param);
    }

    @ApiOperation(value="查询众筹列表占比",response = RewardModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query",name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(paramType = "path",name = "rewardId", value = "悬赏id"),
    })
    @GetMapping("/percentage/{rewardId}")
    public ReturnValue rewardPercentagelist(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize,@PathVariable Integer rewardId) {
        PageHelper.startPage(page, pageSize);
        RewardParam param=new RewardParam(page,pageSize);
        param.setRewardId(rewardId);
        return rewardService.rewardPercentagelist(param);
    }



}
